//
//  XCLogListViewController.m
//
//
//  Created by 帅 on 2022/11/21.
//

#import "XCLogListViewController.h"
#import "XCLogManager.h"
@interface FileModel : NSObject
@property (nonatomic, copy) NSString *fileName;
@property (nonatomic, copy) NSString *filePath;
@end

@implementation FileModel

@end

@interface XCLogListViewController ()
@property (nonatomic, retain) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray* fileListArray;

@end

@implementation XCLogListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    /// 同步日志    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 40, 40);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 100)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    [self loadFileList];
}

- (void)backButtonAction:(UIButton *)button {
    [self.navigationController dismissViewControllerAnimated:true completion:nil];
}

- (void)loadFileList
{
    self.fileListArray = [NSMutableArray array];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *logDirectoryPathUrl = [NSURL URLWithString:[XCLogManager logpath]];
    NSDirectoryEnumerator *logDirectoryEnumerator = [fileManager enumeratorAtURL:logDirectoryPathUrl
                                                   includingPropertiesForKeys:@[NSURLNameKey]
                                                                      options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
                                                                 errorHandler:nil];
    
    for (NSURL *file in logDirectoryEnumerator) {
        NSNumber *isDirectory;
        [file getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
        if (![isDirectory boolValue]) {
            FileModel *model = [FileModel new];
            model.fileName = file.lastPathComponent;
            model.filePath = file.path;
            [self.fileListArray addObject:model];
        }
    }
    
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fileListArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *logFileCellIdentifier = @"logFileCellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:logFileCellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:logFileCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    FileModel* fileModel = [self.fileListArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:fileModel.fileName];

    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    previewController.dataSource = self;
    previewController.delegate = self;
    previewController.currentPreviewItemIndex = indexPath.row;
    [self.navigationController pushViewController:previewController animated:YES];
}

#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return self.fileListArray.count;
}

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    FileModel *model = self.fileListArray[index];
    return [NSURL fileURLWithPath:model.filePath];
}

@end
